//登录校验
$("#login").click(function(){
    username = $("#emplynum").val();
    userpwd = $("#inputpasswd").val();
	$.ajax({
		url: "/validate",
		type: "POST",
		data: {username:username, userpwd:userpwd},

		success: function(data, textStatus, jqXHR){
			res=JSON.parse(data)['result'];
			if (res==true){
				$("#status").text("Init success\n" + $("#status").val());
				$("#login").attr('class', 'btn btn-success');
        //$("#m0").show();
			}
			else{
	      //$("#m0").hide();
				$("#status").text("Init Failed\n" + $("#status").val());
				$("#login").attr('class', 'btn btn-danger');
			}
		},
		
		error:function(jqXHR, textStatus, errorThrown) {
			console.log("request failed " + textStatus);
		}

	});
});


$("#init").click(function(){
	$.ajax({
		url: "/init",
		type: "GET",

		success: function(data, textStatus, jqXHR){
			//console.log(JSON.parse(data)['result']);
			//console.log(textStatus);
			//console.log(jqXHR);
			res=JSON.parse(data)['result'];
			if (res=='success'){
				$("#status").text("Init success\n" + $("#status").val());
				$("#init").attr('class', 'btn btn-success');
			}
			else{
				$("#status").text("Init Failed\n" + $("#status").val());
				$("#init").attr('class', 'btn btn-danger');
			}
		},
		
		error:function(jqXHR, textStatus, errorThrown) {
			console.log("request failed " + textStatus);
		}

	});
});
//点击M0
//
$("#m0").click(function(){
	$.ajax({
		url: "/m0",
		type: "GET",

		success: function(data, textStatus, jqXHR){
			res=JSON.parse(data)['result'];
			if (res=='success'){
				$("#status").text("Init success\n" + $("#status").val());
				$("#m0").attr('class', 'btn btn-success');
        //$("#m0").show();
			}
			else{
	      //$("#m0").hide();
				$("#status").text("Init Failed\n" + $("#status").val());
				$("#m0").attr('class', 'btn btn-danger');
			}
		},
		
		error:function(jqXHR, textStatus, errorThrown) {
			console.log("request failed " + textStatus);
		}

	});
});
//点击M1
$("#m1").click(function(){
	$.ajax({
		url: "/m1",
		type: "POST",

    //ETtype = $("input:checked" ).val(); //抓取用户打标类型选择，1自动，0手动。
		data: {ETtype: 0},
		success: function(data, textStatus, jqXHR){
			res=JSON.parse(data)['result'];
			if (res=='success'){
				$("#status").text("Init success\n" + $("#status").val());
				$("#m1").attr('class', 'btn btn-success');
        //$("#m0").show();
			}
			else{
	      //$("#m0").hide();
				$("#status").text("Init Failed\n" + $("#status").val());
				$("#m1").attr('class', 'btn btn-danger');
			}
		},
		
		error:function(jqXHR, textStatus, errorThrown) {
			console.log("request failed " + textStatus);
		}

	});
});
//点击TX
$("#TX").click(function(){
    tmpln=$("#choosetemplate").val();
    templatestr = $("#choosetemplate").find("option:selected").text();
    //vlist = templatestr.match(/\[(.+?)\]/g);
	$.ajax({
		url: "/tx",
		type: "POST",
		data: {templatestr: templatestr, templaten: tmpln, text: $("#text").val()},
		success: function(data, textStatus, jqXHR){
			res=JSON.parse(data)['result'];
			if (res=='success'){
				$("#status").text("Init success\n" + $("#status").val());
				$("#TX").attr('class', 'btn btn-success');
			}
			else{
				$("#status").text("Init Failed\n" + $("#status").val());
				$("#TX").attr('class', 'btn btn-danger');
			}
		},
		
		error:function(jqXHR, textStatus, errorThrown) {
			console.log("request failed " + textStatus);
		}

	});

});
//选择模版
$("#choosetemplate").change(function(){
	var choice = $(this).val();
	var path = './static/pic/' + choice + '.png';
	$("#status").text("选择模版" + choice + "\n" + $("#status").val());
	$("#template").attr('src',path);
	$("#template").show();
});


